package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.result.CustomizeException;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishMapper dishMapper;
    /*
    * 添加菜品和口味信息
    * */
    @Override
    @Transactional
    public void addTwo(DishDto dishDto) {
        //菜品保存
        this.save(dishDto);

        Long dishId = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (int i = 0; i < flavors.size(); i++) {
            flavors.get(i).setDishId(dishId);
        }

        dishFlavorService.saveBatch(flavors);
    }

    /*
    *根据id查询菜品与口味信息
    * */
    @Override
    public DishDto selectTwo(Long id) {
        Dish dish = this.getById(id);

        //将dish的基本信息拷贝到dishDao中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);

        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /*
    * 修改菜品与口味信息
    * */
    @Override
    public void updateTwo(DishDto dishDto) {
        //更新菜品信息
        this.updateById(dishDto);

        //删除修改之前的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        //添加修改的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (int i = 0; i < flavors.size(); i++) {
            flavors.get(i).setDishId(dishDto.getId());
        }

        dishFlavorService.saveBatch(flavors);
    }

    /*
     * 分页查询菜品与口味
     * */
    @Override
    public R<Page> selevtAll(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page,pageSize);

        List<DishDto> dishDtos = dishMapper.selectAll(pageInfo, name);

        pageInfo.setRecords(dishDtos);

        return R.success(pageInfo);
    }

    /*
    * 修改售卖状态
    * */
    @Override
    public void dishStatus(Integer status, Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            Long id = ids[i];
            //根据id得到每个菜品
            Dish dish = this.getById(id);
            dish.setStatus(status);

            this.updateById(dish);
        }
    }

    /*
    * 删除菜品与口味信息
    * */
    @Override
    @Transactional
    public void dishDelete(List<Long> ids) {
        //查询菜品的状态是否可以删除
        LambdaQueryWrapper<Dish> lqwDish = new LambdaQueryWrapper<>();
        lqwDish.in(Dish::getId, ids).eq(Dish::getStatus, 1);

        int count = this.count(lqwDish);

        if (count > 0) {
            throw new CustomizeException("菜品包含起售状态，不能删除！");
        }
        //删除菜品信息
        this.removeByIds(ids);
        //删除口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, ids);

        dishFlavorService.remove(lqw);
    }


}
